//
//  Loger3DNewUserRegisterVC.m
//  Longer3D
//
//  Created by longer3d on 15/8/6.
//  Copyright (c) 2015年 longer3d. All rights reserved.
//

#import "Loger3DNewUserRegisterVC.h"
#import "NewUserRegisterVIew.h"
//#import "PersonInfo.h"
#import "MBProgressHUD+MJ.h"
//#import "MobClick.h"
#import "PGTabBarController.h"


@interface Loger3DNewUserRegisterVC ()<NewUserRegisterVIewDelegate>

@property (nonatomic,strong)NewUserRegisterVIew *registerView;

@end



@implementation Loger3DNewUserRegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"注册";
    self.registerView = [[NewUserRegisterVIew alloc]initWithFrame: self.view.bounds];
    self.registerView.backgroundColor = publicBackgroundColor;
    self.registerView.delegate = self;
    [self.view addSubview:self.registerView];
    
     self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(backClick) image:@"back" highImage:@"back"];
    
}

- (void)backClick{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - NewUserRegisterVIewDelegate
- (void)registViewSendSmsCode:(NSString *)mobile
{
    NSMutableDictionary *para = [[NSMutableDictionary alloc] init];
    para[@"mobile"] = mobile;
    
    [XYNetworking POST:@"api/getCode" baseURL:LYT_BASE_URL params:para success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary * dict = (NSDictionary*)responseObject;
        LoLog(@"%@",dict);
        if ([dict[@"code"] intValue] == 0) {
            [MBProgressHUD showSuccess:@"验证码发送成功,注意查收!"];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [MBProgressHUD showError:@"请检查网络"];
    }];

}


- (void)registViewRegiterWithUsername:(NSString *)mobile SmsCode:(NSString *)smscode Password:(NSString *)pw RepeatPW:(NSString *)repeatPw
{
    NSMutableDictionary *para = [[NSMutableDictionary alloc] init];
    para[@"mobile"] = mobile;
//    para[@"smsCode"]= smscode;
    para[@"password"] = pw;
    
    [XYNetworking POST:@"api/register" baseURL:LYT_BASE_URL params:para success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = (NSDictionary*)responseObject;
        if ([dict[@"code"] intValue] == 0) {
            [self loginWithMobile:mobile password:pw];
         }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [MBProgressHUD showError:@"请检查网络"];
    }];
    
    
}


- (void)loginWithMobile:(NSString *)mobile password:(NSString *)password{
    
    NSMutableDictionary *para = [[NSMutableDictionary alloc] init];
    para[@"mobile"] = mobile;
    para[@"password"] = password;
    
    [XYNetworking POST:@"api/login" baseURL:LYT_BASE_URL params:para success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = (NSDictionary*)responseObject;
        if ([dict[@"code"] intValue] == 0) {
            //登陆成功,存下token到沙盒,跳转到主页
            [[NSUserDefaults standardUserDefaults] setObject:dict[@"token"] forKey:@"token"];
//            [self.navigationController popViewControllerAnimated:YES];
//            [self.navigationController dismissViewControllerAnimated:YES completion:^{
//                UIWindow *window = [UIApplication sharedApplication].keyWindow;
//                window.rootViewController = [[PGTabBarController alloc] init];
//            }];
            
            [self restoreRootViewController:[[PGTabBarController alloc] init]];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [MBProgressHUD showError:@"请检查网络"];
    }];

}

// 登陆后淡入淡出更换rootViewController
- (void)restoreRootViewController:(UIViewController *)rootViewController
{
    typedef void (^Animation)(void);
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    
    rootViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    Animation animation = ^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        [UIApplication sharedApplication].keyWindow.rootViewController = rootViewController;
        [UIView setAnimationsEnabled:oldState];
    };
    
    [UIView transitionWithView:window
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:animation
                    completion:nil];
}

@end
